package com.example.grafstock_upload;

import org.csi.controle.core.util.RetornoServico;
import org.csi.controle.upload.dados.Dados;
import org.csi.controle.upload.listener.TratarRetornoServico;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity implements OnClickListener {

	static int REQUEST_BAR_CODE_CAPTURE = 49374;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
		Button btn = (Button) this.findViewById(R.id.btnLerCodigo);
		btn.setOnClickListener(this);
		Button btnCam = (Button) this.findViewById(R.id.btnUsarCodigo);
		btnCam.setOnClickListener(this);
		Button btnListaOs = (Button) this.findViewById(R.id.btnListaOs);
		btnListaOs.setOnClickListener(this);
		
	}

	@Override
	public void onClick(View v) {
		String name = getResources().getResourceName(v.getId());
		if(name.contains("btnUsarCodigo")) {
			ServicoAndroid<Long> sa = new ServicoAndroid<Long>(new TratarRetornoServico<Long>() {
				@Override
				public void onRetorno(RetornoServico<Long> retorno) {
					Dados.idOrdemServico = retorno.getData();
				}
				@Override
				public void onFinish() {
				}
				@Override
				public Context getContext() {
					return null;
				}
				
			});
			EditText txtCodigoOs = (EditText) this.findViewById(R.id.editText1);
			String[] params = new String[] {ServicoAndroid.TIPO_OBTER_ID_OS, txtCodigoOs.getText().toString()};
			sa.execute(params);
			
			startActivity(new Intent(v.getContext(), GaleriaActivity.class));
//		    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//		    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
//		        startActivityForResult(takePictureIntent, ImageManager.REQUEST_IMAGE_CAPTURE);
//		    }
		} else if(name.contains("btnLerCodigo")) {
			IntentIntegrator scanIntegrator = new IntentIntegrator(this);
			scanIntegrator.initiateScan();			
		} else if(name.contains("btnListaOs")) {
			startActivity(new Intent(this, ListaOsActivity.class));	
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(requestCode == REQUEST_BAR_CODE_CAPTURE) {
			IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
			String codigo = "";
			if (scanResult != null) {
				codigo = scanResult.getContents();	
			}
			EditText editText = (EditText) this.findViewById(R.id.editText1);
			editText.setText(codigo);
		} else if (requestCode == ImageManager.REQUEST_IMAGE_CAPTURE) {
			Intent i = ImageManager.getInstancia(getResources(), null).tratarRetorno(requestCode, resultCode, data, this);
			startActivity(i);			
		}
	}

}
